﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;

public class DictionaryTips : MonoBehaviour
{
    public enum EnumTest : byte
    {
        A,
        B,
        C,
    }

    /// <summary>
    /// 自定义Comparer
    /// </summary>
    public class EnumTestComparer : IEqualityComparer<EnumTest>
    {
        public bool Equals(EnumTest x, EnumTest y)
        {
            return x == y;
        }

        public int GetHashCode(EnumTest obj)
        {
            return (int)obj;
        }
    }

    private Dictionary<EnumTest, int> m_dicGCEnum;
    private Dictionary<EnumTest, int> m_dicNoGCEnum;
    private Dictionary<int, int> m_dicIntEnum;

    private int m_data;
    
    void Start()
    {
        //实例化m_dicGCEnum
        m_dicGCEnum = new Dictionary<EnumTest, int>();
        //使用自定义的Comparer实例化m_dicNoGCEnum
        m_dicNoGCEnum = new Dictionary<EnumTest, int>(new EnumTestComparer());
        m_dicIntEnum = new Dictionary<int, int>();
    }
    
    void Update()
    {
        //----------------------------------------------//
        Profiler.BeginSample("----- GC Enum ------");

        //20B GC
        m_dicGCEnum.TryGetValue(EnumTest.A, out m_data);

        Profiler.EndSample();
        //----------------------------------------------//





        //----------------------------------------------//
        Profiler.BeginSample("----- No GC Enum ------");

        //No GC
        m_dicNoGCEnum.TryGetValue(EnumTest.A, out m_data);

        Profiler.EndSample();
        //----------------------------------------------//






        //----------------------------------------------//

        Profiler.BeginSample("----- int Dic ------");

        //No GC
        m_dicIntEnum.TryGetValue(1, out m_data);

        Profiler.EndSample();
        //----------------------------------------------//
    }
}
